import plugin from '../../lib/plugins/plugin.js';
import moment from 'moment'
import common from '../../lib/common/Common.js';
/**作者: Admilk
 * 传播请标注作者名字
 * 插件交流群792873018
 */
export class ViewAllTimes extends plugin {
  constructor() {
    super({
      name: '查看所有次数',
      dsc: '查看所有群组的戳一次数',
      priority: -99999999991,
	  event: 'message',
      rule: [
        {
          reg: '^#?(查看|查询)(所有|全部)(次数|戳一戳)$',
          fnc: 'viewAllTimes',
        },
		//{
        //  reg: /^#?测试转发(\d+)$/,
        //  fnc: 'zhuanfa',//
       // },
        {
          reg: '^#?历史(次数|戳一戳)$',
          fnc: 'viewAllallTimes',
        },
        {
          reg: '^#清空戳一戳(次数)?$',
          fnc: 'clearAllTimes'
        }
      ]
    }); 
this.task = [ // 初始化 this.task
  {
	      cron: '5 0 0 * * ?',
    name: '清除缓存的redis',
    fnc: () => this.clearAllTimes(),
  }
];


  }
  async test(){
	  logger.info('aw')
	  this.reply('aw')
  }



  async viewAllTimes(e) {
    // try {
      const keys = await redis.keys('*:cyctimes');
      const messages = ['***全部次数***',];

      for (const key of keys) {
        const value = await redis.get(key);
        const groupId = key.split(':')[0];
        messages.push(`群组 ${groupId} 的戳一次数为 ${value}`);
      }
	messages.push('***这已经是全部次数了***')
      const forward = messages.map(msg => `${msg}\n`);
      const forwardMsg = await common.makeForwardMsg(e, forward, '所有群组戳一次数');

      await this.reply(forwardMsg);
    // } catch (error) {
      // console.error('出错了:', error);
      // e.reply('发生错误，请稍后再试');
    // }
  }
async clearAllTimes(e) {
    const keys = await redis.keys('*:cyctimes');
    const promises = [];

    // 遍历所有键并删除
    for (const key of keys) {
      promises.push(redis.del(key));
    }

    await Promise.all(promises);
	console.log('已清除所有群的戳一次数记录');
}

async viewAllallTimes(e) {
  try {
    const keyss = await redis.get('cycalltimes');
    const msg = `我被戳了 ${keyss} 次`;
    e.reply(msg);
  } catch (error) {
    e.reply('出错了:', error);
  }
}
async clearTimes(e) {
  try {

    const keys = await redis.keys('*:cyctimes');
    const promises = [];

    // 遍历所有键并删除
    for (const key of keys) {
      promises.push(redis.del(key));
    }

    await Promise.all(promises);
    //e.reply('已清除所有群的戳一次数记录');
	console.log('已清除所有群的戳一次数记录');

  } catch (error) {
    console.log('出错了:', error);
  }
}
}
export class chuo extends plugin {
  constructor() {
    super({
      name: '戳一111111',
      dsc: '戳一戳机器人触发效果',
      event: 'notice.group.poke',
      priority: -999999999999991,
      rule: [
        {
          fnc: 'chuoyichu',
        },

      ],

    });
  }
//
  async chuoyichu(e) {
	  if (!e.isGroup) {e.reply('?'); return true}
	  if (e.target_id === e.self_id) {
    try {
      // 获取当前群的戳一次数
	  let allkey = `cycalltimes`;
      let timesKey = `${e.group_id}:cyctimes`; 
      let times = await redis.get(timesKey);
	  let timesall = await redis.get(allkey);
	        if (!timesall) {
        timesall = 0;
        await redis.set(allkey, 0);
      } else {
        timesall = parseInt(timesall);
      }
      if (!times) {
        times = 0;
        await redis.set(timesKey, 0);
      } else {
        times = parseInt(times);
      }


      times++;
      await redis.set(timesKey, times);
	  timesall++;
	  await redis.set(allkey, timesall);

      const msg = `别戳辣！我已经在这个群被戳了 ${times} 次了！！！`;
	  
      e.reply(msg);
	  return false
    } catch (error) {
      console.error('出错了:', error);
      e.reply('发生错误，请稍后再试');
	  return false
    }
	  }else{
	  return false
	  }
  }

async clearTimes(e) {

    const keys = await redis.keys('*:cyctimes');
    const promises = [];

    // 遍历所有键并删除
    for (const key of keys) {
      promises.push(redis.del(key));
    }

    await Promise.all(promises);
console.log('已清除所有群的戳一次数记录');



}
}